package com.huangwei.entity;

import com.huangwei.enumeration.ResponseCode;
import lombok.Data;

import java.io.Serializable;

/*
执行者执行完成或者错误后向消费者返回的结果对象
三个数据：相应状态码：Integer statusCode；相应状态补充信息：String message； 相应数据 T data；增加requestid用于验证
两个静态方法：成功：public static <T> RpcResponse <T> success(T data) ；失败：public static <T> RpcResponse <T> fail(T data)
 */
@Data
public class RpcResponse<T> implements Serializable {
    private String requestId;
    private Integer statusCode;
    private String message;
    private T data;

    public static <T> RpcResponse <T> success(T data,String requestId){
        RpcResponse<T> response = new RpcResponse<T>();
        response.setRequestId(requestId);
        response.setStatusCode(ResponseCode.SUCCESS.getCode());
        response.setData(data);
        return response;

    }

    public static <T> RpcResponse <T> fail(ResponseCode code,String requestId){
        RpcResponse<T> response = new RpcResponse<T>();
        response.setRequestId(requestId);
        response.setStatusCode(code.getCode());
        response.setMessage(code.getMessage());
        return response;
    }

    public RpcResponse(){}
}
